import http from '../../../utils/request.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scoreList: ['', '', '', ''],
        scoreContent: {},//成绩单
        cjpositionlist: {},//成绩定位
        smallquestionlist: [], //小题分
        errorquestionlist: [],//错题分析
        knowledgelist: [],//知识点掌控
        weekmonthlist: [],
        tabindex: 1,
        studentid: '',
        currentShowIndex: -1, // 初始化为 -1，表示没有项被显示
        timer: null, // 用于存储定时器
        currentShowIndex1: -1, // 初始化为 -1，表示没有项被显示
        timer1: null, // 用于存储定时器
        examId: null,
        subjectId: null,
        examdetailId: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.info(options)
        this.scoretranscript(options.examId, options.studentId) //成绩单
        this.scorepositionfixed(options.examId, options.studentId, options.classId) //成绩定位
        this.smallquestionscore(options.examId, options.studentId) //小题分
        this.errorquestionscore(options.examId, options.studentId) //错题分析
        this.knowledgeconetnt(options.examId, options.studentId) //知识点掌控
        this.weekmonth(1, options.studentId) //周月汇总
        this.setData({ studentid: options.studentId, examId: options.examId, subjectId: options.subjectId, examdetailId: options.examDetailId })
    },
    showDeviationInfo() {
        wx.showModal({
            content: '离均差 = 你的成绩 - 平均成绩',
            showCancel: false
        });
    },
    workscore(e) {
        let { studentid } = this.data
        let tabindex = Number(e.currentTarget.dataset.tab)
        this.setData({ tabindex: tabindex })
        this.weekmonth(Number(e.currentTarget.dataset.tab), studentid)
    },
    //成绩单
    async scoretranscript(examid, studentid) {
        let options = {
            url: `/score/student/transcript/${examid}`,
            method: 'get',
            data: { studentId: studentid },
            header: { "content-type": "application/json;charset=UTF-8" }
        }
        let res = await http(options)
        console.info(res)
        if (res.data.code == 200) {
            this.setData({ scoreContent: res.data.data })
        } else {

        }
    },
    //成绩定位
    async scorepositionfixed(examid, studentid, classid) {
        let options = {
            url: `/score/student/class-position/${examid}`,
            method: 'get',
            data: { studentId: studentid, classId: classid },
            header: { "content-type": "application/json;charset=UTF-8" }
        }
        let res = await http(options)
        console.info(res)
        if (res.data.code == 200) {
            this.setData({ cjpositionlist: res.data.data })
        } else {

        }
    },
    //小题分
    async smallquestionscore(examid, studentid) {
        let options = {
            url: `/score/student/question-scores/${examid}`,
            method: 'get',
            data: { studentId: studentid },
            header: { "content-type": "application/json;charset=UTF-8" }
        }
        let res = await http(options)
        console.info(res)
        if (res.data.code == 200) {
            this.setData({ smallquestionlist: res.data.data.questions })
        } else {

        }
    },
    // 错题分析
    async errorquestionscore(examid, studentid) {
        let options = {
            url: `/score/student/error-questions/${examid}`,
            method: 'get',
            data: { studentId: studentid },
            header: { "content-type": "application/json;charset=UTF-8" }
        }
        let res = await http(options)
        console.info(res)
        if (res.data.code == 200) {
            const originalArray = res.data.data; // 假设这是你的原始数组
            const newArray = originalArray.map(item => {
                return {
                    ...item, // 使用展开运算符保留原对象的所有属性
                    contentishow: false // 添加新属性
                };
            });
            this.setData({ errorquestionlist: newArray })
        } else {

        }
    },
    // 知识点掌控
    async knowledgeconetnt(examid, studentid) {
        let options = {
            url: `/score/student/knowledge-score/${examid}`,
            method: 'get',
            data: { studentId: studentid },
            header: { "content-type": "application/json;charset=UTF-8" }
        }
        let res = await http(options)
        if (res.data.code == 200) {
            const originalArray = res.data.data; // 假设这是你的原始数组
            const newArray = originalArray.map(item => {
                return {
                    ...item, // 使用展开运算符保留原对象的所有属性
                    contentishow: false // 添加新属性
                };
            });
            this.setData({ knowledgelist: newArray })
        } else {

        }
    },
    // 周月总会
    async weekmonth(tabindex, studentid) {
        console.info(tabindex)
        if (tabindex === 1) {
            let options = {
                url: `/score/student/summary`,
                method: 'get',
                data: { studentId: studentid, summaryType: 'week' },
                header: { "content-type": "application/json;charset=UTF-8" }
            }
            let res = await http(options)
            console.info(res)
            if (res.data.code == 200) {
                this.setData({ weekmonthlist: res.data.data })
            } else {

            }
        } else {
            let options = {
                url: `/score/student/summary`,
                method: 'get',
                data: { studentId: studentid, summaryType: 'month' },
                header: { "content-type": "application/json;charset=UTF-8" }
            }
            let res = await http(options)
            console.info(res)
            if (res.data.code == 200) {
                this.setData({ weekmonthlist: res.data.data })
            } else {

            }
        }

    },
    contentxswz(e) {
        let index = e.currentTarget.dataset.index
        let { errorquestionlist, currentShowIndex } = this.data;

        // 创建一个新数组来更新数据
        let newErrorQuestionList = [...errorquestionlist];

        // 隐藏之前显示的项
        if (currentShowIndex !== -1) {
            newErrorQuestionList[currentShowIndex].contentishow = false;
        }

        // 显示当前点击的项
        newErrorQuestionList[index].contentishow = true;

        // 更新数据
        this.setData({
            errorquestionlist: newErrorQuestionList,
            currentShowIndex: index // 更新当前显示的索引
        });
    },
    contentxswz2(e) {
        let index = e.currentTarget.dataset.index
        let { errorquestionlist, currentShowIndex } = this.data;

        // 创建一个新数组来更新数据
        let newErrorQuestionList = [...errorquestionlist];

        // 隐藏之前显示的项
        if (currentShowIndex !== -1) {
            newErrorQuestionList[currentShowIndex].contentishow = true;
        }

        // 显示当前点击的项
        newErrorQuestionList[index].contentishow = false;

        // 更新数据
        this.setData({
            errorquestionlist: newErrorQuestionList,
            currentShowIndex: index // 更新当前显示的索引
        });
    },
    contentxswz1(e) {
        let index = e.currentTarget.dataset.index
        let { knowledgelist, currentShowIndex1, timer1 } = this.data;

        // 清除之前的定时器
        if (timer1) {
            clearTimeout(timer1);
        }

        // 创建一个新数组来更新数据
        let newErrorQuestionList = [...knowledgelist];

        // 隐藏之前显示的项
        if (currentShowIndex1 !== -1) {
            newErrorQuestionList[currentShowIndex1].contentishow = false;
        }

        // 显示当前点击的项
        newErrorQuestionList[index].contentishow = true;

        // 更新数据
        this.setData({
            knowledgelist: newErrorQuestionList,
            currentShowIndex1: index // 更新当前显示的索引
        });

        // 设置定时器，几秒后隐藏当前显示的内容
        this.setData({
            timer1: setTimeout(() => {
                newErrorQuestionList[index].contentishow = false;
                this.setData({
                    knowledgelist: newErrorQuestionList,
                    currentShowIndex1: -1 // 重置当前显示的索引
                });
            }, 3000) // 5秒后自动隐藏
        });
    },
    roulinkerror() {
        let { studentid, examId, subjectId, examdetailId } = this.data
        wx.navigateTo({
            url: '/pages/homework/errorwork/errorwork?examId=' + examId + '&studentId=' + studentid + '&subjectId=' + subjectId + '&examDetailId=' + examdetailId,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        const { timer, timer1 } = this.data;
        if (timer) {
            clearTimeout(timer);
        }
        if (timer1) {
            clearTimeout(timer1);
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})